load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"

location_f="/Users/ivana/WORK_BSC/AS/files/"

fsw    = addfile(location_f + "surfnextflux_lowice_ipsl.nc", "r")
fsc    = addfile(location_f + "surfnextflux_ctrl_ipsl.nc", "r")

ftw    = addfile(location_f + "toanetfluxes_lowice_ipsl.nc", "r")
ftc    = addfile(location_f + "toanetfluxes_ctrl_ipsl.nc", "r")

;*******************************************;
;Read in variables
;*******************************************
lat = fsc->lat
rad  = 4.0 * atan(1.0) / 180.
clat=cos(lat*rad)

toasc=ftc->tops(0,:,:)
toasw=ftw->tops(0,:,:)

toalc=ftc->topl(0,:,:)
toalw=ftw->topl(0,:,:)

toanetc= toasc
toanetc= toasc-toalc ; positive downward
toanetw= toasw
toanetw= toasw-toalw

toanetc_z=toanetc(:,0)
toanetc_z=dim_avg(toanetc)

toanetw_z=toanetw(:,0)
toanetw_z=dim_avg(toanetw)

anot=toanetw_z
anot=(toanetw_z-toanetc_z)*clat ; normalize by the cosine of latitude

;print(anot)
;exit

surfnetc = fsc->bils(0,:,:)
surfnetw = fsw->bils(0,:,:)

surfnetc_z= surfnetc(:,0)
surfnetc_z = dim_avg(surfnetc)

surfnetw_z= surfnetw(:,0)
surfnetw_z = dim_avg(surfnetw)

anos = surfnetw_z
anos = (surfnetw_z-surfnetc_z)*clat*(-1.) ; change sign to be postive upward

anoc=anot
anoc=anot+anos
print(anoc)

;****************************************
; Create plot
;****************************************
wks = gsn_open_wks("pdf","zonal_netflux_ipsl")

   plot    = new (2,"graphic")
  
   res                      = True          ; individual plot
   res@gsnDraw              = False
   res@gsnFrame             = False
   res@xyLineThicknessF     = 2.0
   res@xyMonoLineColor         =True
   res@xyLineColor         = "black"
   res@gsnYRefLine           = 0.0             ; create a reference line  
   res@gsnAboveYRefLineColor = "red"              ; above ref line fill red
   res@gsnBelowYRefLineColor = "blue"             ; below ref line fill blue

   res@xyDashPattern        = 0                  ; Make curves all solid

   res@vpHeightF= 0.4                    ; change aspect ratio of plot
   res@vpWidthF = 0.8 
 
   res@trYMinF=-1
   res@trYMaxF=1.5
   
   res@tiXAxisFontHeightF = 0.010
   res@tiYAxisFontHeightF = 0.015

   res@gsnLeftString = ""
   res@tiYAxisString = "TOA net flux ano" 
   res@tiXAxisString = ""

   plot(0)  = gsn_csm_xy (wks,lat,anot,res)
   res@tiYAxisString = "Column net flux ano"   
   plot(1)  = gsn_csm_xy (wks,lat,anoc,res) 
 

;***** make panel ***** 
   

   resP                     = True          ; panel resources
   resP@gsnMaximize         = True
   
   resP@txFontHeightF = 0.015 
   gsn_panel(wks,plot,(/2,1/),resP)
